//
//  TwoTableView.m
//  PerfectWorld
//
//  Created by dllo on 16/9/26.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "TwoTableView.h"
#import "DecorateSonViewController.h"
#import "MapDepotTableViewCell.h"
#import "NetworkHandler.h"
#import "MapDepotModel.h"
#import "StrategyList.h"
#import "MapUser.h"

@interface TwoTableView ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)MapDepotModel *model;
@end

@implementation TwoTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self handData];
        [self reloadData];
        self.delegate = self;
        self.dataSource = self;
        [self registerNib:[UINib nibWithNibName:@"MapDepotTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    }
    return self;
}
- (void)handData {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [user objectForKey:@"dic"];
    NSDictionary *dicUser = [dic objectForKey:@"user"];
    NSString *userName = [dicUser objectForKey:@"userName"];
    NSString *strURL = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/user/%@/strategylikes?start=0&count=5", userName];
    NSLog(@"%@", strURL);
    [NetworkHandler getDataByUrlString:strURL BodyString:nil WithDataBlock:^(id result) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:result];
        self.model = [MapDepotModel modelWithDic:dic];
        [self reloadData];
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.bounds.size.height / 5;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DecorateSonViewController *vc = [[DecorateSonViewController alloc] init];
    vc.urlId = _model.strategyList[indexPath.row].Map_id.description;
    [self.pushTWODelegate pushTWOViewController:vc id:_model.strategyList[indexPath.row].Map_id];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _model.strategyList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MapDepotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.strategyList = _model.strategyList[indexPath.row];
    return cell;
}


@end
